package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EUDataGridResult;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
import com.taotao.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 商品管理service
 * Created by marui on 2017/6/13.
 */
@Service
public class TbItemServiceImpl implements TbItemService {

	@Autowired
	private TbItemMapper tbItemMapper;

	@Override
	public TbItem getItemById(long itemId) {
//		TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
		TbItemExample example = new TbItemExample();
		TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(itemId);
		List<TbItem> tbItems = tbItemMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(tbItems)){
			return null;
		}
		return tbItems.get(0);
	}

	/**
	 * 商品列表查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public EUDataGridResult getItemlist(int page, int rows) {
		//查询商品列表
		TbItemExample tbItemExample = new TbItemExample();
		//分页处理
		PageHelper.startPage(page, rows);
		List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample);
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(tbItems);
		//取记录总条数
		PageInfo<TbItem> pageInfo = new PageInfo<>(tbItems);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

}
